package com.coderxq.blog.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.File;

@Configuration
public class MyWebMvcConfigurer  implements WebMvcConfigurer  {
    @Autowired
    private JwtInterceptor jwtInterceptor;


    //添加请求拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //通过registry对象可以注册任意个实现了HandlerInterceptor接口的请求拦截器对象 可以实现拦截访问指定路径的请求或者排除指定路径的请求拦截
        registry.addInterceptor(jwtInterceptor).addPathPatterns("/**")
                .excludePathPatterns("/User/login/*","/User/login/**","/User/registerUser/*",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/**",
                        "/swagger-ui.html/**","/doc.html");

    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:" + System.getProperty("user.dir") + File.separator + "images" + File.separator);
        registry.addResourceHandler("/template/**")
                .addResourceLocations("file:" + System.getProperty("user.dir") + File.separator + "template" + File.separator);
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
    }

    @Override//处理跨域请求
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**") // 允许跨域访问的路径
                .allowedOrigins("*")  // 允许跨域访问的源
                .allowedHeaders("*")
                .allowedMethods("*") // 允许请求方法
                .allowCredentials(false); // 是否发送 Cookie
    }


    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 使用setAllowedOrigin会出现IllegalArgumentException
        config.addAllowedOriginPattern("*");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config);
        // 使用FilterRegistrationBean包装CorsFilter
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        // 设置Filter的优先级，值越小优先级越高
        bean.setOrder(0);
        return bean;
    }

}
